package com.example.mybatisfinaltask.controller;

import com.example.mybatisfinaltask.pojo.Emp;
import com.example.mybatisfinaltask.pojo.Result;
import com.example.mybatisfinaltask.service.EmpService;
import com.example.mybatisfinaltask.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class LoginController {
    @Autowired
    private EmpService empService;
    @PostMapping("/login")
    Result login(@RequestBody Emp emp){
        Emp e=empService.vertifyLogin(emp);
        if(e!=null){
            Map<String ,Object>claims=new HashMap<>();
            claims.put("id",e.getId());
            claims.put("name",e.getName());
            claims.put("username",e.getUsername());
            String jwt= JwtUtils.generateJwt(claims);
            log.info("登录请求成功，令牌是{}",jwt);
            System.out.println();
            System.out.println("在此处给出令牌");
            return  Result.success(jwt);
        }
        else{
            log.info("登录请求失败");
            return Result.error("用户名或密码错误");
        }
    }

}
